/**
 * 
 */
package com.kandinfo.base.common.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kandinfo.base.common.util.AppConfigUtil;

/**
 * jdbc connection util
 * @author tangqh
 */
public class JdbcConnection {
	private static Logger log = LoggerFactory.getLogger(JdbcConnection.class);
	
	private static String getUrl() {
		return "jdbc:mysql://115.29.211.158:3306/k12_tiku2.1_exp?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";
	}
	private static String getUser() {
		return "k12_tkdb";
	}
	private static String getPassword() {
		return "123456";
	}
	private static String getDriverClass() {
		return "com.mysql.jdbc.Driver";
	}
	
	
	private static DataSource dataSource;
	
	
	public static DataSource getDataSource() {
		return dataSource;
	}
	public static void setDataSource(DataSource dataSource) {
		JdbcConnection.dataSource = dataSource;
	}
	
	
	
	/**
	 * 获取程序配置的默认连接
	 */
	public static Connection getConnection() {
		try {
			return getDataSource().getConnection();
		} catch (Exception e) {
			return getConnection(getUrl(), getUser(), getPassword(), getDriverClass());
		}
	}
	
	/**
	 * 获取指定地址的MySql数据库连接
	 */
	public static Connection getMySqlConnection(String jdbcUrl, String jdbcUser, String jdbcPassword) {
		try {
			return getConnection(jdbcUrl, jdbcUser, jdbcPassword, getDriverClass());
		} catch (Exception e) {
			return getConnection(getUrl(), getUser(), getPassword(), getDriverClass());
		}
	}
	
	/**
	 * 获取连接
	 */
	public static Connection getConnection(String jdbcUrl, String jdbcUser, String jdbcPassword, String driverClass) {
		Connection conn = null;
		try {
			if (jdbcUrl == null) {
				jdbcUrl = AppConfigUtil.getConfig("jdbc.jdbcUrl");
			}
			if (jdbcUser == null) {
				jdbcUser = AppConfigUtil.getConfig("jdbc.user");
			}
			if (jdbcPassword == null) {
				jdbcPassword = AppConfigUtil.getConfig("jdbc.password");
			}
			if (driverClass == null) {
				driverClass = AppConfigUtil.getConfig("jdbc.driverClass");
			}
			Class.forName(driverClass);
			conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword);
		} catch (ClassNotFoundException e) {
			log.error("jdbc Driver Class NotFoundException[driverClass:"+driverClass+"]", e);
		} catch (SQLException e) {
			log.error("get jdbc exception, info[jdbcUrl:"+jdbcUrl+",jdbcUser:"+jdbcUser+",jdbcPassword:"+(jdbcPassword!=null?jdbcPassword:"")+"]", e);
		}
		return conn;
	}
	
	/**
	 * 关闭连接
	 */
	public static void close(ResultSet resultSet) {
		close(resultSet, null, null);
	}

	
	/**
	 * 关闭连接
	 */
	public static void close(ResultSet resultSet, Statement statement) {
		close(resultSet, statement, null);
	}
	
	/**
	 * 关闭连接
	 */
	public static void close(ResultSet resultSet, Statement statement, Connection connection) {
		try {
			if (resultSet != null) {
				resultSet.close();
			}
			if (statement != null) {
				statement.close();
			}
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			log.error("jdbc connection close exception", e);
		}
	}
	
	/**
	 * 创建PreparedStatement
	 * 
	 * @param connection
	 * @param sql
	 * @param resultSetType
	 * @param resultSetConcurrency
	 * @param resultSetHoldability
	 * @return
	 */
	public static PreparedStatement getPreparedStatement(Connection connection,String sql,Integer resultSetType,Integer resultSetConcurrency,Integer resultSetHoldability){
		PreparedStatement preparedStatement = null;
		if (connection == null) {
			return preparedStatement;
		}
		try {
			if (resultSetHoldability != null) {
				preparedStatement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
			}else if (resultSetType != null && resultSetConcurrency != null) {
				preparedStatement = connection.prepareStatement(sql, resultSetType, resultSetConcurrency);
			}else {
				preparedStatement = connection.prepareStatement(sql);
			}
		} catch (SQLException e) {
			log.error("jdbc PreparedStatement create exception", e);
		}
		return preparedStatement;
	}
}
